<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
    <title>ShellCheck: SC1102 – Shells disambiguate `$((` differently or not at all. For `$(command substitution)`, add space after `$(` . For `$((arithmetics))`, fix parsing errors.</title>
    <link rel="stylesheet" href="css/bootstrap.min.css" />
  </head>
  <body style="margin-left: auto; margin-right: auto; max-width: 800px">
    <h1>SC1102 – ShellCheck Wiki</h1>
    <a href="https://github.com/koalaman/shellcheck/wiki/SC1102">See this page on GitHub</a>
    <p style="display: none"><a href="index.html">Sitemap</a></p>
    <hr />
    <h2
id="shells-disambiguate--differently-or-not-at-all-for-command-substitution-add-space-after---for-arithmetics-fix-parsing-errors">Shells
disambiguate <code>$((</code> differently or not at all. For
<code>$(command substitution)</code>, add space after <code>$(</code> .
For <code>$((arithmetics))</code>, fix parsing errors.</h2>
<h3 id="problematic-code">Problematic code:</h3>
<div class="sourceCode" id="cb1"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb1-1"><a href="SC1102.html#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="bu">echo</span> <span class="st">&quot;</span><span class="va">$(</span><span class="kw">(</span><span class="ex">cmd</span> <span class="st">&quot;</span><span class="va">$@</span><span class="st">&quot;</span><span class="kw">)</span> <span class="dv">2</span><span class="op">&gt;&amp;</span><span class="dv">1</span><span class="va">)</span><span class="st">&quot;</span></span></code></pre></div>
<h3 id="correct-code">Correct code:</h3>
<div class="sourceCode" id="cb2"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb2-1"><a href="SC1102.html#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="bu">echo</span> <span class="st">&quot;</span><span class="va">$(</span> <span class="kw">(</span><span class="ex">cmd</span> <span class="st">&quot;</span><span class="va">$@</span><span class="st">&quot;</span><span class="kw">)</span> <span class="dv">2</span><span class="op">&gt;&amp;</span><span class="dv">1</span><span class="va">)</span><span class="st">&quot;</span></span></code></pre></div>
<h3 id="rationale">Rationale:</h3>
<p>You appear to be using <code>$((</code> with two (or more)
parentheses in a row, where the first <code>$(</code> should open a
subshell.</p>
<p>This is an ill-defined structure that is parsed differently between
different shells and shell versions. Prefer adding spaces to make it
unambiguous, both to shells and humans.</p>
<p>Consider the <code>$(((</code> in <code>$(((1)) )</code>:</p>
<p>Ash, dash and Bash 1 parses it as <code>$(( (</code> and subsequently
fail to find the matching <code>))</code>. Zsh and Bash 2+ looks ahead
and parses it as <code>$( ((</code>. Ksh parses it as
<code>$( ( (</code>.</p>
<h3 id="exceptions">Exceptions:</h3>
<p><strong>Alternatively</strong>, you may indeed have correctly spaced
your parentheses, but ShellCheck failed to parse <code>$((</code> as an
arithmetic expression while accidentally succeeding in parsing it as
<code>$(</code> + <code>(</code>.</p>
<p>In these cases, double check the syntax to ensure ShellCheck can
parse the <code>$((</code>, or ignore this error and hope that it won't
affect analysis too severely.</p>
    <hr />
    <p style='font-size: 80%'><a href="../index.html">ShellCheck</a> is a static analysis tool for shell scripts. This page is part of its documentation.</p>
  </body>
</html>


